//
// Created by Administrator on 2023/8/3.
//
#include <iostream>
#include <vector>

using namespace std;


class Solution {
public:
    int semiOrderedPermutation(vector<int> &nums) {
        // 找到1 和 n 的位置 x,y
        // 把1挪到最左边需要x次，把n挪到最右边需要n-y-1次
        int pos1 = -1, posn = -1, n = nums.size();
        for (int i = 0; i < nums.size(); i++) {
            if (nums[i] == 1) {
                pos1 = i;
            } else if (nums[i] == n) posn = i;
        }
        // 如果1在n的右边，则结果-1
        int ans = 0;
        if (pos1 > posn ) ans--;
        ans = ans + n + pos1 - posn - 1;
        return ans;
    }

};

int main() {
    Solution s;
    vector<int> nums1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    vector<int> nums2 = {2, 1, 4, 3};
    vector<int> nums3 = {2, 4, 1, 3};
    vector<int> nums4 = {1, 3, 4, 2, 5};
    cout << s.semiOrderedPermutation(nums1) << endl;
    cout << s.semiOrderedPermutation(nums2) << endl;
    cout << s.semiOrderedPermutation(nums3) << endl;
    cout << s.semiOrderedPermutation(nums4) << endl;
    return 0;
}